package handler;

import java.io.File;

import java.util.Locale;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import windows.Ferestre;

import data.Angajat;
import data.AngajatiData;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.Number;

public class ExportFiles {
	private Ferestre fereastra;

	public ExportFiles(Ferestre ferestre) {

		fereastra = ferestre;

	}

	public void setFile(AngajatiData angajati) throws Exception {
		JFileChooser fc = new JFileChooser();
		File file;
int i=0;
		FileNameExtensionFilter filter = new FileNameExtensionFilter("xls",
				"All files");
		fc.addChoosableFileFilter(filter);
		int returnVal = fc.showSaveDialog(fereastra.getReportGeneratorFrame()
				.getFrame());
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			File file1 = new File(file.getPath() + ".xls");

			
			WorkbookSettings ws = new WorkbookSettings();
			ws.setLocale(new Locale("ro", "RO"));
			WritableWorkbook workbook = Workbook.createWorkbook(file1, ws);
			WritableSheet s = workbook.createSheet("Sheet1", 0);
		
			WritableCellFormat cf1obj = new WritableCellFormat(
					NumberFormats.INTEGER);
			for(Angajat angajat:angajati.getSelectedSchool().getListaAngajati())
			{
				Label c1 = new Label(0,i, angajat.getNumeAngajat());
				Label c2 = new Label(1,i,angajat.getContAngajat());
				Number c3 = new Number(2, i,angajat.getSuma(), cf1obj);
				
			
			s.addCell(c1);
			s.addCell(c2);
			s.addCell(c3);
			i++;
			}
			workbook.write();
			workbook.close();
		}
	}
}
